package com.example.mansystem.controller;


import com.example.mansystem.dto.*;
import com.example.mansystem.entity.*;
import com.example.mansystem.exception.CodeAndMsg;
import com.example.mansystem.exception.UniformReponseHandler;
import com.example.mansystem.exception.UserDefinedException;
import com.example.mansystem.mapper.FieldNameMapper;
import com.example.mansystem.mapper.InstrumentEditCacheMapper;
import com.example.mansystem.mapper.InstrumentMapper;
import com.example.mansystem.service.ActService;
import com.example.mansystem.service.InstrumentService;
import com.example.mansystem.service.RoleService;
import com.example.mansystem.service.WebSocketServer;
import com.example.mansystem.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/instrument")
public class InstrumentController {
    @Resource
    FieldNameMapper fieldNameMapper;
    @Resource
    InstrumentMapper instrumentMapper;
    @Resource
    InstrumentEditCacheMapper instrumentEditCacheMapper;
    @Autowired
    RoleService roleService;
    @Autowired
    InstrumentService instrumentService;
    @Autowired
    ActService actService;

    @GetMapping("/fieldname")
    public CallResultMsg GetFieldCnName()
    {
        List<FieldNameDO> res=fieldNameMapper.selectList(null);
        return new UniformReponseHandler<List<FieldNameDO>>().sendSuccessResponse(res);
    }

    @PostMapping ("/getInstrument")
    public CallResultMsg GetInstrumentDoById(@RequestBody @Validated GetInstrumentDto validator)
    {
        InstrumentDO res=instrumentMapper.selectById(validator.getYQBH());
        if(res == null)
            throw  new UserDefinedException(CodeAndMsg.InstrumentNotFound);
        return new UniformReponseHandler<InstrumentDO>().sendSuccessResponse(res);
    }

    @PostMapping("/getInstrumentCache")
    public CallResultMsg getInstrumentCache(@RequestBody @Validated GetInstrumentDto validator)
    {
        InstrumentCaCheDO res = instrumentEditCacheMapper.selectById(validator.getYQBH());
        if(res == null)
            throw new UserDefinedException(CodeAndMsg.InstrumentNotFound);
        return new UniformReponseHandler<InstrumentCaCheDO>().sendSuccessResponse(res);
    }

    @PostMapping("/getPassInstrument")
    public CallResultMsg GetPassInstrument(@RequestBody @Validated PageInstrumentDto validator)
    {
        PageInstrument res=instrumentService.getPageInstrument(validator);
        return new UniformReponseHandler<PageInstrument>().sendSuccessResponse(res);
    }

    @PostMapping("/InstrumentExamine")
    public CallResultMsg changeInstrumentExamine(@RequestBody @Validated ChangeInstrumentExamineDto validator)
    {
        actService.ExaminePass(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @PostMapping("/register")
    public CallResultMsg InstrumentRegister(@RequestBody @Validated InstrumentRegisterDto validator)
    {
        instrumentService.register(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @PostMapping("/batchRegister")
    public CallResultMsg batchRegister(HttpServletRequest request,@RequestBody @Validated BatchInstrumentDto validator) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        instrumentService.batchRegister(username,validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @PostMapping("/registerEdit")
    public CallResultMsg InstrumentRegisterEdit(@RequestBody @Validated InstrumentRegisterEditDto validator)
    {
        instrumentService.registerEdit(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @PostMapping("/instrumentEdit")
    public CallResultMsg instrumentEdit(@RequestBody @Validated InstrumentEditDto validator)
    {
        instrumentService.instrumentEdit(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @GetMapping("/allexamine")
    public CallResultMsg getAllExamineInfo(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        String roleName = roleService.getRoleNameByUsername(username);
        ExamineInfo res=instrumentService.getAllExamineInfo(roleName);
        return new UniformReponseHandler<ExamineInfo>().sendSuccessResponse(res);
    }

    @GetMapping("/myexamine")
    public CallResultMsg getMyExamineInfo(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        return new UniformReponseHandler<ExamineInfo>().sendSuccessResponse(actService.getEditList(username));
    }

    @GetMapping("/examineDegree")
    public CallResultMsg getExamineDegree()
    {
        ExamineDegreeVO res=instrumentService.getExamineDegree();
        return new UniformReponseHandler<ExamineDegreeVO>().sendSuccessResponse(res);
    }

    @PostMapping("/changeDegree")
    public CallResultMsg changeExamineDegree(@RequestBody ExamineDegreeVO validator)
    {
        instrumentService.changeExamineDegree(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @GetMapping("/getPresentData")
    public CallResultMsg getPresentData()
    {
        InPresentVO res=instrumentService.getInPresent();
        return new UniformReponseHandler<InPresentVO>().sendSuccessResponse(res);
    }

    @PostMapping("/deleteInstrument")
    public CallResultMsg deleteInstrument(@RequestBody DeleteInDto validator)
    {
        instrumentService.deleteIn(validator);
        return new UniformReponseHandler<String>().sendSuccessResponse("SUCCESS");
    }

    @GetMapping("/getMyExamineHis")
    public CallResultMsg getMyExamineHis(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<ExamineHisVO> res = instrumentService.getExamineHisByUsername(username);
        return new UniformReponseHandler<List<ExamineHisVO>>().sendSuccessResponse(res);
    }

    @GetMapping("/homeData")
    public CallResultMsg getHomeData()
    {
        HomeDataVO res = instrumentService.getHomeData();
        return new UniformReponseHandler<HomeDataVO>().sendSuccessResponse(res);
    }
}
